package com.yanqu.road.dao.impl.platform;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.annualreport.AnnualReportData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AnnualReportDaoImpl extends TempDao {

    public Map<Long, AnnualReportData> getAnnualReportDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_annual_report";
        Map<Long, AnnualReportData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                AnnualReportData data = new AnnualReportData();
                data.setUserId(rs.getLong("userId"));
                String reportData = rs.getString("reportData");
                if(StringUtils.isNullOrEmpty(reportData)){
                    reportData = "{}";
                }
                Map<Integer, String> tempMap = JSON.parseObject(reportData, new TypeReference<Map<Integer, String>>() {});
                data.setReportDataMap(tempMap);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}